﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Diagnostics;

namespace TfsExtensions.Build.Activities
{
    public sealed class InvokeXslTransformForManifest : CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> SandcastleTransformsFolder { get; set; }

        [RequiredArgument]
        public InArgument<string> SandcastleProductionToolsFolder { get; set; }

        [RequiredArgument]
        public InArgument<string> WorkingFolderName { get; set; }

        [RequiredArgument]
        public InArgument<string> SandcastleFormatXsl { get; set; }

        [RequiredArgument]
        public InArgument<string> ManifestFilename { get; set; }

        [RequiredArgument]
        public InArgument<string> ReflectionXMLFilename { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            //<Exec Command="&quot;$(TransformEXE)&quot; /xsl:&quot;$(SandcastleTransformsFolder)\ReflectionToManifest.xsl&quot; $(WorkingFolderName)\$(ReflectionXMLFilename) /out:$(WorkingFolderName)\$(ManifestFilename)" />

            var exeFileName = string.Format(@"{0}\XslTransform.exe", this.SandcastleProductionToolsFolder.Get(context));

            var transformsFolder = "/xsl:\"" + string.Format(@"{0}\ReflectionToManifest.xsl", this.SandcastleTransformsFolder.Get(context)) + "\"";

            var workingFolder = "\"" + string.Format(@"{0}\{1}", this.WorkingFolderName.Get(context), this.ReflectionXMLFilename.Get(context)) + "\"";

            var outputPath = "\"" + string.Format("{0}\\{1}", this.WorkingFolderName.Get(context), this.ManifestFilename.Get(context)) + "\"";

            var arguments = string.Format("{0} {1} /out:{2}", transformsFolder, workingFolder, outputPath);

            try
            {
                InvokeCommand.ExecuteCommand(context, exeFileName, arguments);
            }
            catch(Exception ex)
            {
                LogHelper.LogError(context, ex.Message);
                throw;
            }
        }
    }
}
